<template>
  <div>
    <!-- 轮播图的地方 -->
    <div class="swiper-container" id="mySwiper">
      <div class="swiper-wrapper">
        <div
          class="swiper-slide"
          v-for="(carousel, index) in bannerList"
          :key="index"
        >
          <img :src="carousel.imgUrl" />
        </div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>

      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
  </div>
</template>

<script>
// 捞取仓库的数据
import { mapState } from "vuex";
import Swiper from "swiper";
export default {
  name: "Carousel",
  mounted() {
    // 派发action，通知vuex获取数据
    this.$store.dispatch("getBannerList");
    // mounted是组件加载完毕，结构有了
  },
  computed: {
    // 组件身上有数据
    ...mapState({
      // key名bannerList
      bannerList: (state) => state.home.bannerList,
    }),
  },
  updated() {
    var mySwiper = new Swiper(".swiper-container", {
      loop: true, // 循环模式选项
      autoplay: {
        disableOnInteraction: false,
        delay: 2000, //2秒切换一次
      },

      // 如果需要分页器
      pagination: {
        el: ".swiper-pagination",
      },

      // 如果需要前进后退按钮
      navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
      },
    });
  },

  methods: {},
};
</script>

<style lang="less" scoped>
.swiper-container {
  width: 1372.69px;
}
img {
  width: 1372.69px;
  height: 550px;
}
</style>